package com.portal.back.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.portal.back.service.ISysContentService;
import com.portal.back.service.impl.SysContentService;
import com.portal.base.web.BaseAction;
import com.portal.domain.SysContent;
import com.portal.util.common.CharacterUtil;

/**
 * @Title:后台内容Action
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-7
 * 
 */
@SuppressWarnings("serial")
public class SysContentAction extends BaseAction {
	private ISysContentService service;

	public SysContentAction() {
		service = new SysContentService();
	}

	private SysContent syscontent;

	/**
	 * 增加或修改公司简介 syscontent.code=1
	 * 
	 * @return
	 */
	public String addOrEditCompanyMes() {
		syscontent.setTitle("公司简介");
		syscontent.setCode("1");
		syscontent.setContent(CharacterUtil.encode(syscontent.getContent()));
		boolean isFlag = service.addOrEditCompany(syscontent);
		if (isFlag)
			return "success";
		else
			return "error";
	}

	/**
	 * 查出公司简介信息
	 * 
	 * @return
	 */
	public String getCompanyMes() {
		SysContent s = service.getComany();
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", s);
		return "getCompany";
	}

	/**
	 * 增加发展历史或专业荣誉
	 * 
	 * @return
	 */
	public String getDev() {

		String code = syscontent.getCode();
		if ("3".equals(code))
			syscontent.setTitle("发展历程");
		if ("4".equals(code))
			syscontent.setTitle("专业荣誉");
		syscontent.setContent(CharacterUtil.encode(syscontent.getContent()));
		syscontent.setDateString(CharacterUtil.encode(syscontent
				.getDateString()));
		boolean isFlag = service.addDev(syscontent);
		if (isFlag) {
			response.setContentType("text/html");
			try {
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				String bath = request.getContextPath();
				out
						.println("<script>alert('保存成功');window.parent.location.href='"
								+ bath + "/content/getAllDev.shtml';</script>");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		} else
			return "error";

	}

	/**
	 * 增加相应的新闻信息  code=5 集团新闻 code=6 区域公司新闻  code=7 行业资讯
	 */
	public String addnew() {
		boolean isFlag = service.addOrEditNew(syscontent);
		if (isFlag)
			return "success";
		else
			return "error";
	}

	/**
	 * 获得新闻信息列表
	 */
	public String getNew() {
		List<SysContent> list = service.getNewContent();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllnew";
	}

	/**
	 * 获得资讯顾问 营销代理 信息
	 */
	public String getCustom() {
		List<SysContent> list = service.getCustomContent();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllcustom";
	}

	/**
	 * 获得发展历史或专业荣誉列表
	 * 
	 * @return
	 */
	public String getAllDev() {
		List<SysContent> list = service.getDevContent();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllDev";
	}

	/**
	 * 获得 专业研究 企业内刊
	 */
	public String getAllPaper() {
		List<SysContent> list = service.getPaperContent();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllPaper";
	}

	/**
	 * 获得文化和招聘信息
	 */
	public String getAllCulture() {
		List<SysContent> list = service.getCultureContent();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllCulture";
	}

	/**
	 * 删除发展历史 或专业荣誉
	 * 
	 * @return
	 */
	public String deleteContent() {
		String deleteByIdString = request.getParameter("id");
		boolean isFlag = service.deleteById(deleteByIdString);
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = null;
		if (isFlag)
			message = "删除成功";
		else
			message = "删除失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获得某个发展历程或专业荣誉
	 * 
	 * @return
	 */
	public String getSomeById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "somecontent";
	}

	/**
	 * 获得某个新闻id
	 * 
	 * @return
	 */
	public String getNewById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "somenew";
	}

	/**
	 * 获得某个资讯顾问 营销代理
	 * 
	 * @return
	 */
	public String getCustomById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "somecustom";
	}

	/**
	 * 获得专业研究 企业内刊
	 * 
	 * @return
	 */
	public String getpaperById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "somepaper";
	}

	/**
	 * 获得某个客户信息
	 */
	public String getguestById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "someguest";
	}

	/**
	 * 获得某个招聘信息
	 */
	public String getcultureById() {
		String id = request.getParameter("id");
		SysContent content = service.getById(id);
		ActionContext context = ActionContext.getContext();
		context.put("syscontent", content);
		return "someculture";
	}

	/**
	 * 增加资讯顾问 营销代理
	 * 
	 * @return
	 */
	public String addcustom() {
		String code = syscontent.getCode();
		if ("8".equals(code))
			syscontent.setTitle("咨询顾问");
		if ("9".equals(code))
			syscontent.setTitle("营销代理");

		syscontent.setContent(CharacterUtil.encode(syscontent.getContent()));
		boolean isFlag = service.addOrEditNew(syscontent);
		if (isFlag)
			return "success";
		else
			return "error";
	}

	/**
	 * 增加 专业研究 企业内刊
	 * 
	 * @return
	 */
	public String addpaper() {
		String code = syscontent.getCode();
		if ("10".equals(code))
			syscontent.setTitle("专业研究");
		if ("11".equals(code))
			syscontent.setTitle("企业内刊");

		syscontent.setContent(CharacterUtil.encode(syscontent.getContent()));
		boolean isFlag = service.addOrEditNew(syscontent);
		if (isFlag)
			return "success";
		else
			return "error";
	}

	/**
	 * 增加企业文化信息
	 * 
	 * @return
	 */
	public String addculture() {
		String code = syscontent.getCode();
		if ("14".equals(code))
			syscontent.setTitle("企业文化理念");
		if ("15".equals(code))
			syscontent.setTitle("校园招聘");

		syscontent.setContent(CharacterUtil.encode(syscontent.getContent()));
		boolean isFlag = service.addOrEditNew(syscontent);
		if (isFlag)
			return "success";
		else
			return "error";
	}

	public SysContent getSyscontent() {
		return syscontent;
	}

	public void setSyscontent(SysContent syscontent) {
		this.syscontent = syscontent;
	}

}
